package com.shushan.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;


import com.shushan.base.Constants;
import com.shushan.home.TabHomeActivity;
import com.shushan.lockscreen.GamesWindowManager;
import com.shushan.service.ForegroundRTAService;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.util.DateUtils;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.SystemUtils;


/**
 * @ClassName: FreeLockReceiver
 * @Desciption: 小孩桌面非自由到时广播
 * @author: yichaohua
 * @date: 2018-03-16
 */
public class FreeLockReceiver extends BroadcastReceiver {

    private static final String TAG = "FreeLockReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG,"onReceive:" + DateUtils.getNowDate());

        sendClearBroad(context);
        SharedPreferencesUtils.setIntDate(Constants.EXIST_GAME_TIME_SHAREPREFERS_KEY
                + BabyApplication.getInstance().getCurrentChildId(),0);

        boolean isChildControl = SharedPreferencesUtils.getBooleanDate(Constants.IS_CHILD_CONTROL_SHAREPREFERS_KEY);
        Log.i(TAG,"isChildControl=" + isChildControl);
        if(!isChildControl){
            Log.e(TAG,"isChildControl != false");
            return;
        }

        SystemUtils.sendForegActivity(context,TabHomeActivity.class);

//        DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(DEVICE_POLICY_SERVICE);
//        ComponentName adminComponent = new ComponentName(context, LockAdminReceiver.class);
//        if (dpm.isAdminActive(adminComponent)) {
//            dpm.lockNow();
//        }else {
//            LockScreenWindowManager.createLockFloatWindow(context);
//        }
    }

    private void sendClearBroad(Context context) {
        Intent intent = new Intent();
        intent.putExtra(Constants.CLEAR_WARN_DIALOG_KEY,Constants.CLEAR_WARN_DIALOG_KEY);
        intent.setAction(Constants.FREE_WARN__RECEIVER_ACTION);
        context.sendBroadcast(intent);

        GamesWindowManager.removeGamesFloatWindow(BabyApplication.getInstance());

        Intent intent1 = new Intent(context,ForegroundRTAService.class);
        intent1.putExtra("isStopSelf",true);
        context.startService(intent1);
    }
}
